﻿using HMEngine.HMComponents;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HMEngine.HMCameras {
    public sealed class HMCamera : IHMUpdatable {
        private Viewport myViewport;

        public Vector3 Position { get; set; }
        public Quaternion Rotation { get; set; }
        public Matrix World { get; private set; }
        public Matrix View { get; private set; }
        public Matrix Projection { get; private set; }
        public float FieldOfView { get; set; }
        public Vector3 Target { get; set; }

        public Viewport Viewport {
            get { return myViewport; }
            set {
                myViewport = value;
                myViewport.MinDepth = 1.0f;
                myViewport.MaxDepth = 1000.0f;
            }
        }

        public HMCamera(Viewport newViewport) {
            Position = new Vector3(0, 0, 1);
            Rotation = new Quaternion(0, 0, 0, 1);
            FieldOfView = MathHelper.Pi / 3.0f;
            Target = new Vector3(0, 0, 0);

            Viewport = newViewport;
        }

        public void Update(GameTime gameTime) { UpdateMatrices(); }

        private void UpdateMatrices() {
            World = Matrix.Identity;

            View = Matrix.Invert(Matrix.CreateFromQuaternion(Rotation) * Matrix.CreateTranslation(Position));

            Projection = Matrix.CreatePerspectiveFieldOfView(FieldOfView, Viewport.AspectRatio, Viewport.MinDepth, Viewport.MaxDepth);
        }
    }
}